using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.DirectX;	 
using Microsoft.DirectX.Direct3D;
using System.Windows.Forms;
using System.Drawing;
using IngenEngine.Meshes;

namespace IngenEngine
{
	public class Engine
	{
		private Camera camera;
		private MemoryManager memoryManager;
		private Device device;
		public delegate void Render();
		private Form mainForm;

		public Device Device
		{
			get { return device; }
			set { device = value; }
		}

		public Engine(Form mainForm)
		{
			this.mainForm = mainForm;

			InitializeGraphics();
		}

		/// <summary>
		/// Sets up and configures the main device. This is the core of the engine.
		/// </summary>
		public void InitializeGraphics()
		{
			//Init configuration
			Config.SetPresentParameters(mainForm);
			Config.SetVertexFormat();

			//Init device
			device =
				new Device(0,								//Which adapter to use (0, usually)
					DeviceType.Hardware,					//Which type to use (software would be rendered by the cpu, for example)
					mainForm,								//The container to render in
					CreateFlags.HardwareVertexProcessing,	//Device settings, in this case forcing Vertex processing to hardware
					Config.presentParams);					//Presentation settings, in this forcing windowed mode, and SwapEffect.Discard
				Config.SetRenderState(ref device);

			//Configure device's default camera position
			Config.SetCameraInformation();
			camera = new Camera(ref device);
		}

		/// <summary>
		/// Prepares the device for rendering, renders all objects, and presents the scene to the user.
		/// </summary>
		/// <param name="method">Anything being rendered is passed via a delegate into the proper position for renderingb</param>
		public void Run(Render method)
		{
			device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Gray, 1.0f, 0);
			device.BeginScene();
			method();
			device.EndScene();
			device.Present();
		}
	}
}